package com.uddenberg.wt;

import java.util.ArrayList;

import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

public class InsertGroupActivity extends ActivityGroup {

	// Keep this in a static variable to make it accessible for all the nesten activities, lets them manipulate the view  
    public static InsertGroupActivity group;  
  
        // Need to keep track of the history if you want the back-button to work properly, don't use this if your activities requires a lot of memory.  
    private ArrayList<View> history; 
    
    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.layout.menu, menu);
	    return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	        case R.id.icontext: Toast.makeText(this, "You pressed the icon and text!", Toast.LENGTH_LONG).show();
	                            break;
	    }
	    return true;
	}
    
    
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
    	super.onCreate(savedInstanceState);  
    	this.history = new ArrayList<View>();  
        group = this;  
    	
    	 
	    View view =	getLocalActivityManager().startActivity("InsertGeneralActivity", 
				new Intent(this, InsertGeneralActivity.class)
	    		.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))  
	            .getDecorView();  
	    // Replace the view of this ActivityGroup  
	    replaceView(view);
    }
     
    public void back() {  
    	if(history.size() > 1) {  
    		
    		history.remove(history.size()-1);  
        	setContentView(history.get(history.size()-1));
        	
        } else {  
            finish();  
        }  
    }  
  
   @Override  
    public void onBackPressed() {  
	   	InsertGroupActivity.group.back();  
        return;  
    } 
    
    
    public void replaceView(View v) {  
        // Adds the old one to history  
    	history.add(v);  
        // Changes this Groups View to the new View.  
    	setContentView(v);  
	} 
}
